package com.zrwl.poscloud.xo.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrwl.poscloud.base.entity.*;
import com.zrwl.poscloud.base.enums.CompanyServerBusinessType;
import com.zrwl.poscloud.base.enums.EStatus;
import com.zrwl.poscloud.base.superI.SuperServiceImpl;
import com.zrwl.poscloud.commons.utils.HTTPUtils;
import com.zrwl.poscloud.commons.utils.RedisUtil;
import com.zrwl.poscloud.commons.utils.ResultUtil;
import com.zrwl.poscloud.commons.utils.StringUtils;
import com.zrwl.poscloud.xo.global.MessageConf;
import com.zrwl.poscloud.xo.global.RedisConf;
import com.zrwl.poscloud.xo.global.SQLConf;
import com.zrwl.poscloud.xo.global.SysConf;
import com.zrwl.poscloud.xo.mapper.ServerSyncRecordMapper;
import com.zrwl.poscloud.xo.service.*;
import com.zrwl.poscloud.xo.vo.ServerSyncRecordVO;
//import com.zrwl.poscloud.xo.vo.ServerSyncVersionVO;
import org.apache.http.client.config.RequestConfig;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.util.*;

/**
 * 服务器业务同步记录 服务实现类
 *
 * @author 刘思童 AutoGenerate
 * @date 2022-03-26
 */
@Service
public class ServerSyncRecordServiceImpl extends SuperServiceImpl<ServerSyncRecordMapper, ServerSyncRecord> implements ServerSyncRecordService {

    @Autowired
    private ServerSyncRecordService serverSyncRecordService;
    @Resource
    private ServerSyncRecordMapper serverSyncRecordMapper;

    @Override
    public IPage<ServerSyncRecord> getPageList(ServerSyncRecordVO serverSyncRecordVO) {
        QueryWrapper<ServerSyncRecord> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(serverSyncRecordVO.getKeyword()) && !StringUtils.isEmpty(serverSyncRecordVO.getKeyword().trim())) {
            queryWrapper.like("name", serverSyncRecordVO.getKeyword().trim());
        }
        Page<ServerSyncRecord> page = new Page<>();
        page.setCurrent(serverSyncRecordVO.getCurrentPage());
        page.setSize(serverSyncRecordVO.getPageSize());
        queryWrapper.ne(SQLConf.STATUS, EStatus.DISABLED);
        queryWrapper.orderByDesc(SQLConf.CREATE_TIME);
        IPage<ServerSyncRecord> pageList = serverSyncRecordService.page(page, queryWrapper);
        List<ServerSyncRecord> list = pageList.getRecords();
        pageList.setRecords(list);
        return pageList;
    }

    @Override
    public List<ServerSyncRecord> getAllList(ServerSyncRecordVO serverSyncRecordVO) {
        QueryWrapper<ServerSyncRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne(SQLConf.STATUS, EStatus.DISABLED);
        queryWrapper.orderByAsc(SQLConf.CREATE_TIME);
        List<ServerSyncRecord> list = serverSyncRecordService.list(queryWrapper);
        return list;
    }

    @Override
    public String add(ServerSyncRecordVO serverSyncRecordVO) {
        ServerSyncRecord entity = new ServerSyncRecord();
        if (serverSyncRecordVO.getJsonData() == null && serverSyncRecordVO.getObject() != null) {//其他接口调用时通过塞入的对象进行统一转换//上传的数据直接进行插入
            serverSyncRecordVO.setJsonData(JSON.toJSONString(serverSyncRecordVO.getObject()));
        }
        BeanUtils.copyProperties(serverSyncRecordVO, entity, SysConf.STATUS);
        entity.insert();
        serverSyncRecordVO.setUid(entity.getUid());
        return ResultUtil.successWithMessage(MessageConf.INSERT_SUCCESS);
    }

    @Override
    public String edit(ServerSyncRecordVO serverSyncRecordVO) {
        ServerSyncRecord entity = serverSyncRecordService.getById(serverSyncRecordVO.getUid());
        BeanUtils.copyProperties(serverSyncRecordVO, entity);
        entity.setUpdateTime(new Date());
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    @Override
    public String delete(ServerSyncRecordVO serverSyncRecordVO) {
        ServerSyncRecord entity = serverSyncRecordService.getById(serverSyncRecordVO.getUid());
        entity.setStatus(EStatus.DISABLED);
        entity.setUpdateTime(new Date());
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    @Override
    public Map<Integer, List<ServerSyncRecord>> getCloudSyncDataList() {//[此方法为server端使用非云端]//这里是单商户也就是companyUid相同不同则需要对map再嵌套一层companyMAP(目前不处理这种特殊情况),对应下方解析也需要改变
        List<ServerSyncRecord> resultList = serverSyncRecordMapper.getCloudSyncDataList();
        Map<Integer, List<ServerSyncRecord>> resultMap = new HashMap<>();
        if (resultList != null && resultList.size() > 0) {
            for (ServerSyncRecord items : resultList) {
                if (resultMap.get(items.getType()) != null) {
                    resultMap.get(items.getType()).add(items);
                } else {
                    List<ServerSyncRecord> list = new ArrayList<>();
                    list.add(items);
                    resultMap.put(items.getType(), list);
                }
            }
            return resultMap;
        } else {
            return null;
        }
    }

}
